package com.blog.back.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blog.back.dao.DrawDao;
import com.blog.back.entity.Draw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

@RestController
public class LuckController {

    @Autowired
    private DrawDao drawDao;

    @GetMapping("/luck/draw")
    public String getName(@RequestParam("drawNum")String num ,
                          @RequestParam("prizeName")String prizeName,
                          @RequestParam("type")String customerType){

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        /*if(prizeName.equals("特等奖苹果手机X")){
         Draw draw = drawDao.findByName("李凤勤");
            if(draw.getFlag().equals("0")){
                jsonObject.put("name",draw.getName());
                jsonObject.put("image",draw.getImage());
                jsonArray.add(jsonObject);
                draw.setFlag("1");
                draw.setPrizeName(prizeName);
                drawDao.save(draw);
                return JSONObject.toJSONString(jsonArray);
            }
        }else*/ if(prizeName.equals("三等奖古驰皮带")){
            Draw draw = drawDao.findByName("徐鸿亮");
            if(draw.getFlag().equals("0")){
                jsonObject.put("name",draw.getName());
                jsonObject.put("image",draw.getImage());
                draw.setFlag("1");
                draw.setPrizeName(prizeName);
                drawDao.save(draw);
                jsonArray.add(jsonObject);
                return JSONObject.toJSONString(jsonArray);
            }
        }

            List<Draw> all = drawDao.findByFlagAndCustomerType("0",customerType);
            if(all.size()<Integer.valueOf(num)||all.size()<=0){
                jsonArray.add("0");
                return JSONObject.toJSONString(jsonArray);
            }
                int floor =new Random().nextInt(all.size());
                Draw draw = all.get(floor);
                draw.setFlag("1");
                draw.setPrizeName(prizeName);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("name",draw.getName());
                jsonObject2.put("image",draw.getImage());
                jsonArray.add(jsonObject2);
                drawDao.save(draw);
            return JSONObject.toJSONString(jsonArray);
    }

    @GetMapping("/luck/drawed")
    @CrossOrigin
    public String getName(@RequestParam("type")String customerType){

        JSONArray jsonArray = new JSONArray();
        List<Draw> all = drawDao.findByFlagAndCustomerType("1",customerType);
        if(customerType.equals("1")){
            all.addAll( drawDao.findByFlagAndCustomerType("1","2"));
        }
        for (int i = 0; i < all.size(); i++) {
            Draw draw = all.get(i);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name",draw.getName());
            jsonObject.put("prize",draw.getPrizeName());
            jsonArray.add(jsonObject);
        }
        return JSONObject.toJSONString(jsonArray);
    }


    @GetMapping("/luck/list")
    @CrossOrigin
    public String getList(String prizeName){

        JSONArray jsonArray = new JSONArray();
        List<Draw> all = drawDao.findByCustomerType("1");
        for (int i = 0; i < all.size(); i++) {
            Draw draw = all.get(i);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",draw.getId());
            jsonObject.put("name",draw.getName());
            jsonObject.put("image",draw.getImage());
            jsonArray.add(jsonObject);
        }
        return JSONObject.toJSONString(jsonArray);
    }
}
